﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using HBPVPDruid.Misc;

namespace HBPVPDruid
{
    // This is going to be most complicated part of coding,
    // due to so many factors
    //
    //
    // We want to
    // #1 priority ( Flag Carrier [ CanSee(traceline), In Range] ) if is one
    // #2 Loop threw every player (put dots on em)
    // #3 when all have dots, find lowest hp person, then we rape him?

    public static class GetTargetPlayer
    {
        public static bool SingleTarget = false;

        public static WoWPlayer TargetPlayer()
        {
            // Reset this
            SingleTarget = false;
            WoWPlayer Player = null;

            // If there is a flag carrier return the flag carrier
            if (ObjectManager.Me.IsHorde) { Player = EnemyHordeFlagCarrier(); }
            if (ObjectManager.Me.IsAlliance) { Player = EnemyAllianceFlagCarrier(); }
            if (Player != null) Misc.Misc.DebugLog("[Target] Targeting Flag Carrier");
            if (Player != null) SingleTarget = true;
            if (Player != null) return Player;

            // If enemy needed dot
            Player = PlayerToDot();
            if (Player != null) Misc.Misc.DebugLog("[Target] Targeting Random Player To Dot");
            if (Player != null) return Player;

            // Single Target spam
            Player = EnemyLowestHealth();
            SingleTarget = true;
            if (Player != null) Misc.Misc.DebugLog("[Target] Targeting Single Player");
            return Player;

        }


        #region GetPlayer
        private static WoWPlayer EnemyAllianceFlagCarrier()
        {
            return (from Unit in ObjectManager.GetObjectsOfType<WoWPlayer>(false)
                         where Unit.IsAlive
                         where Unit.Distance < 35
                         where !Unit.IsFriendly
                         where !Unit.IsPet
                         where Misc.Misc.HasSpellID(23333)
                         where Unit.InLineOfSight
                         select Unit).FirstOrDefault();
        }

        private static WoWPlayer EnemyHordeFlagCarrier()
        {
            return (from Unit in ObjectManager.GetObjectsOfType<WoWPlayer>(false)
                         where Unit.IsAlive
                         where Unit.Distance < 40
                         where !Unit.IsFriendly
                         where !Unit.IsPet
                         where Misc.Misc.HasSpellID(23335)
                         where Unit.InLineOfSight
                         select Unit).FirstOrDefault();
        }

        private static WoWPlayer EnemyLowestHealth()
        {
               return (from Unit in ObjectManager.GetObjectsOfType<WoWPlayer>(false)
                         where Unit.IsAlive
                         where Unit.Distance < 40
                         where !Unit.IsFriendly
                         where !Unit.IsPet
                         where Unit.InLineOfSight
                         select Unit).FirstOrDefault(); 
        }

        private static WoWPlayer PlayerToDot()
        {
            return (from Unit in ObjectManager.GetObjectsOfType<WoWPlayer>(false)
                         orderby Unit.HealthPercent
                         where Unit.IsAlive
                         where Unit.Distance < 40
                         where !Unit.IsFriendly
                         where !Unit.IsPet
                         where Unit.InLineOfSight
                         where !Unit.HasAura("Insect Swarm") && !Unit.HasAura("Moonfire") 
                         select Unit).FirstOrDefault();
        }
        #endregion

    }
}
